<?php
/**
 * ============================================================================
 * Copyright (c) 2014 WWW.DSXCMS.COM All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0
 * ---------------------------------------------
 * $Date: 2014-07-02
 * $ID: class.ParseVideoUrl.php
 */
namespace App\Helpers;

class VideoParserModel{
    public $vid;
    public $img;
    public $tit;
    public $url;
    public $swf;
}

class VideoParser{
	const URL_VILID = '/(youku\.com|v\.qq\.com)/';
	static function parse($url=''){
		preg_match(self::URL_VILID, strtolower($url),$matches);
		switch ($matches[1]){
			case 'youku.com' :
				$data = self::_youku($url);
				break;
			case 'v.qq.com':
				$data = self::_qq($url);
				break;
			default:
				$data = false;
		}
		return $data;
	}

    /**
     * @param $url
     * @return bool|VideoParserModel
     */
    public static function _youku($url){
		preg_match('/id\_(\w+)[\=|\.html]/', $url,$matches);
		$link = "https://openapi.youku.com/v2/videos/show_basic.json?video_id={$matches[1]}&client_id=3d01f04416cbe807";
		$json = Http::curlGet($link);
		if($json){
			$array = json_decode($json, true);
			$video = new VideoParserModel();
			$video->img = $array['thumbnail'];
			$video->tit = $array['title'];
			$video->url = $array['link'];
			$video->swf = $array['player'];
            return $video;
		}else {
		    return false;
        }
	}


    /**
     * @param $url
     * @return bool|VideoParserModel
     */
    public static function _qq($url){
        preg_match('/\/([a-zA-Z0-9]+)\.html/', $url,$matches);
        if (isset($matches[1])) {
            $vid = $matches[1];

            $xml = Http::curlGet('http://vv.video.qq.com/getinfo?otype=xml&vids='.$vid);
            //将XML转为array
            $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            if (isset($data['vl']['vi']['ti'])) {
                $video = new VideoParserModel();
                $video->vid = $vid;
                $video->url = $url;
                $video->tit = $data['vl']['vi']['ti'];
                $video->img = 'http://vpic.video.qq.com/'.date('mdHi').'/'.$vid.'_160_90_3.jpg';
                $video->swf = 'http://static.video.qq.com/TPout.swf?vid='.$vid;
                return $video;
            }else {
                return false;
            }
        } else {
            return false;
        }
	}
}
